package com.runvp.rest.biz;

import java.util.List;

import com.runvp.common.annotation.ParamValid;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.runvp.common.annotation.Log;
import com.runvp.common.enums.BusinessType;
import com.runvp.service.biz.domain.BizGoodsCategory;
import com.runvp.service.biz.service.IBizGoodsCategoryService;
import com.runvp.common.core.controller.BaseController;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.utils.poi.ExcelUtil;
import com.runvp.common.core.page.TableDataInfo;

/**
 * 商品分类Controller
 * 
 * @author runvp
 * @date 2022-09-18
 */
@Controller
@RequestMapping("/biz/category")
public class BizGoodsCategoryController extends BaseController {
    private String prefix = "biz/category";

    @Autowired
    private IBizGoodsCategoryService bizGoodsCategoryService;

    @RequiresPermissions("biz:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("biz:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizGoodsCategory bizGoodsCategory) {
        startPage();
        bizGoodsCategory.setTenantId(getUserIdFilterAdmin());
        List<BizGoodsCategory> list = bizGoodsCategoryService.selectBizGoodsCategoryList(bizGoodsCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("biz:category:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizGoodsCategory bizGoodsCategory) {
        bizGoodsCategory.setTenantId(getUserIdFilterAdmin());
        List<BizGoodsCategory> list = bizGoodsCategoryService.selectBizGoodsCategoryList(bizGoodsCategory);
        ExcelUtil<BizGoodsCategory> util = new ExcelUtil<BizGoodsCategory>(BizGoodsCategory.class);
        return util.exportExcel(list, "商品分类数据");
    }

    /**
     * 新增商品分类
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品分类
     */
    @RequiresPermissions("biz:category:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizGoodsCategory bizGoodsCategory) {
        bizGoodsCategory.setTenantId(getUserId());
        return toAjax(bizGoodsCategoryService.insertBizGoodsCategory(bizGoodsCategory));
    }

    /**
     * 修改商品分类
     */
    @RequiresPermissions("biz:category:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BizGoodsCategory bizGoodsCategory = bizGoodsCategoryService.selectBizGoodsCategoryById(id);
        mmap.put("bizGoodsCategory", bizGoodsCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品分类
     */
    @ParamValid
    @RequiresPermissions("biz:category:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizGoodsCategory bizGoodsCategory) {
        return toAjax(bizGoodsCategoryService.updateBizGoodsCategory(bizGoodsCategory));
    }

    /**
     * 删除商品分类
     */
    @RequiresPermissions("biz:category:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bizGoodsCategoryService.deleteBizGoodsCategoryByIds(ids));
    }
}
